/*
 * Created by JFormDesigner on Tue Nov 10 16:35:16 CET 2009
 */

package at.jku.pervasive.gui.jformdesigner;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

import at.jku.pervasive.Application;
import at.jku.pervasive.Position;
import at.jku.pervasive.gui.model.RecentTagsTableModel;
import at.jku.pervasive.gui.renderer.LastSeenTableCellRender;

/**
 * @author SHOCKIE
 */
public class TagActivityPanel extends JPanel
{
	private RecentTagsTableModel recentTagsTableModel;

	private boolean altPressed;
	private int clickedRow;

	public TagActivityPanel()
	{
		initComponents();

		recentTagActivitiesTable.addKeyListener(new KeyAdapter()
		{

			@Override
			public void keyReleased(KeyEvent e)
			{
				// clear the selection, if ESC is pressed
				if (KeyEvent.VK_ESCAPE == e.getKeyCode())
				{
					recentTagActivitiesTable.clearSelection();
				}
			}
		});

		final ListSelectionModel selectionModel = recentTagActivitiesTable.getSelectionModel();
		selectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		selectionModel.addListSelectionListener(new ListSelectionListener()
		{
			public void valueChanged(ListSelectionEvent e)
			{
				// is the event finished ?
				if (e.getValueIsAdjusting())
					return;

				// get all selected row indices
				int[] selectedRows = recentTagActivitiesTable.getSelectedRows();

				// get all positions for the tags
				Map<String, List<Position>> tagPositions = recentTagsTableModel.getPositions(selectedRows);

				// tell view3D to update
				Application.getInstance().fireShowTagPositions(tagPositions);
			}
		});

		// recentTagActivitiesTable.addMouseListener(new MouseAdapter()
		// {
		// private List<String> allTags = new LinkedList<String>();
		//
		// @Override
		// public void mouseClicked(MouseEvent e)
		// {
		// altPressed = e.isAltDown();
		// clickedRow = recentTagActivitiesTable.rowAtPoint(e.getPoint());
		//
		// System.out.println("altPressed: " + (altPressed ? "yes" : "no"));
		// System.out.println("clickedRow: " + clickedRow);
		//
		// int[] selctedRows = recentTagActivitiesTable.getSelectedRows();
		//
		// String tagId = (String)
		// recentTagActivitiesTable.getValueAt(clickedRow, 0);
		// if (altPressed)
		// {
		// if (allTags.contains(tagId))
		// {
		// recentTagActivitiesTable.clearSelection();
		// allTags.remove(tagId);
		// System.out.println("remove all tags with id " + tagId);
		//
		// // iterate over all rows
		// for (int i = 0; i < selctedRows.length; i++)
		// {
		// String tmpTagId = (String) recentTagActivitiesTable.getValueAt(i, 0);
		// if (!tagId.equals(tmpTagId))
		// {
		// recentTagActivitiesTable.addRowSelectionInterval(i, i);
		// }
		// }
		// }
		// else
		// {
		// allTags.add(tagId);
		// System.out.println("add all tags with id " + tagId);
		// }
		// }
		//
		// // get all selected row indices
		// int[] selectedRows = recentTagActivitiesTable.getSelectedRows();
		// System.out.println(Arrays.toString(selectedRows));
		//
		// System.out.println("---");
		// }
		// });

		recentTagsTableModel = new RecentTagsTableModel(50);
		recentTagActivitiesTable.setModel(recentTagsTableModel);
		recentTagActivitiesTable.setDefaultRenderer(Date.class, new LastSeenTableCellRender());

		TableColumnModel colModel = this.recentTagActivitiesTable.getColumnModel();
		colModel.getColumn(1).setMinWidth(45);
		colModel.getColumn(1).setMaxWidth(45);
		colModel.getColumn(2).setMinWidth(45);
		colModel.getColumn(2).setMaxWidth(45);
		colModel.getColumn(3).setMinWidth(45);
		colModel.getColumn(3).setMaxWidth(45);
		colModel.getColumn(4).setMinWidth(110);
		colModel.getColumn(4).setMaxWidth(110);
	}

	private void initComponents()
	{
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Open Source Project license - unknown
		JScrollPane recentTagsScrollPane = new JScrollPane();
		recentTagActivitiesTable = new JTable();

		// ======== this ========
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(new BorderLayout());

		// ======== recentTagsScrollPane ========
		{
			recentTagsScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

			// ---- recentTagActivitiesTable ----
			recentTagActivitiesTable.setModel(new DefaultTableModel(new Object[][] {
					{ "020000111241", "23,90", "12,90", "40,90", "18:19:20.999" }, { null, null, null, null, null }, },
					new String[] { "Tag-ID", "X", "Y", "Z", "last seen" }));
			{
				TableColumnModel cm = recentTagActivitiesTable.getColumnModel();
				cm.getColumn(1).setMinWidth(45);
				cm.getColumn(1).setMaxWidth(45);
				cm.getColumn(1).setPreferredWidth(45);
				cm.getColumn(2).setMinWidth(45);
				cm.getColumn(2).setMaxWidth(45);
				cm.getColumn(2).setPreferredWidth(45);
				cm.getColumn(3).setMinWidth(45);
				cm.getColumn(3).setMaxWidth(45);
				cm.getColumn(3).setPreferredWidth(45);
				cm.getColumn(4).setMinWidth(110);
				cm.getColumn(4).setMaxWidth(110);
				cm.getColumn(4).setPreferredWidth(110);
			}
			recentTagActivitiesTable.setPreferredScrollableViewportSize(new Dimension(300, 300));
			recentTagsScrollPane.setViewportView(recentTagActivitiesTable);
		}
		add(recentTagsScrollPane, BorderLayout.CENTER);
		// JFormDesigner - End of component initialization
		// //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY
	// //GEN-BEGIN:variables
	// Generated using JFormDesigner Open Source Project license - unknown
	private JTable recentTagActivitiesTable;

	// JFormDesigner - End of variables declaration //GEN-END:variables

	public void addTagPosition(Position newPosition)
	{
		recentTagsTableModel.addTagPosition(newPosition);
	}

	public void clearAllTagPositions()
	{
		recentTagsTableModel.clearAllTagPositions();
	}

	public void setTagPositions(Position[] positions)
	{
		recentTagsTableModel.setTagPositions(positions);
	}
}
